﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.Company
{
    public partial class SendMoney : System.Web.UI.Page
    {
        private string touserid = "";
        private string companyid = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            touserid = Request.QueryString["touserid"] + "";
            companyid = Request.QueryString["companyid"] + "";
            if (!Page.IsPostBack)
            {
                LoadContent();
            }
        }

        #region 加载内容
        private void LoadContent()
        {
            Users uinfo = UserBase.GetCurrentUser();
            UsersHelper uhelper = new UsersHelper();
            float currentmoney = uhelper.GetCurrentMoney(uinfo.ID);
            litCurrentMoney.Text = currentmoney.ToString();
            hidCurrentMoney.Value = currentmoney.ToString();

            Users uinfoto = uhelper.GetUsers(touserid);
            hidToUserID.Value = touserid;
            litNickName.Text = uinfoto.NickName;
        }
        #endregion

        #region 提交
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Users uinfo=UserBase.GetCurrentUser();

            string sendmoney = txtSendMoney.Text.Trim();
            float sendmoneyfloat = (float)Convert.ToDouble(sendmoney);
            if (sendmoneyfloat <= 0)
            {
                Tools.Alert("工资金额请输入大于0的数");
                return;
            }
            else
            {
                string currentmoney = hidCurrentMoney.Value;
                float currentmoneyfloat = (float)Convert.ToDouble(currentmoney);
                if (sendmoneyfloat > currentmoneyfloat)
                {
                    Tools.Alert("工资金额不能大于可用友赢豆");
                    return;
                }
                else
                {
                    string touserid = hidToUserID.Value;
                    if (touserid == uinfo.ID)
                    {
                        Tools.Alert("不能给自己发工资");
                        return;
                    }
                    else
                    {
                        //得到小组信息
                        Companys cinfo = new Companys();
                        CompanysHelper chelper = new CompanysHelper();
                        cinfo = chelper.GetCompanys(companyid);
                        if (cinfo.OwnerUserID == uinfo.ID)
                        {
                            //插入现金流
                            CashFlowHelper cfhelper = new CashFlowHelper();
                            CashFlow cfinfo1 = new CashFlow();
                            cfinfo1.UsersID = touserid;
                            cfinfo1.Money = sendmoneyfloat;
                            cfinfo1.Type = 9;
                            cfinfo1.Description = "小组<a href=\"../c/"+companyid+".aspx\" target=\"_blank\">"+cinfo.CompanyName+"</a>发工资";
                            cfhelper.AddCashFlow(cfinfo1);

                            CashFlow cfinfo2 = new CashFlow();
                            cfinfo2.UsersID = uinfo.ID;
                            cfinfo2.Money = sendmoneyfloat * (-1);
                            cfinfo2.Type = 9;
                            cfinfo2.Description = "小组<a href=\"../c/" + companyid + ".aspx\" target=\"_blank\">" + cinfo.CompanyName + "</a>给<a href=\"../u/"+touserid+".aspx\" target=\"_blank\">" + litNickName.Text + "</a>发工资";
                            cfhelper.AddCashFlow(cfinfo2);

                            //给该成员发送消息
                            Message minfo = new Message();
                            MessageHelper mhelper = new MessageHelper();
                            minfo.ToUsersID = touserid;
                            minfo.Flag = 0;
                            minfo.FromUsersID = uinfo.ID;
                            minfo.FromNickName = uinfo.NickName;
                            minfo.Status = 0;
                            minfo.MessageContent = "小组<a href=\"../c/" + companyid + ".aspx\" target=\"_blank\">" + cinfo.CompanyName + "</a>发工资了，赶快去查看<a href=\"../UserCash/CashFlowList.aspx\">我的现金流</a>";
                            mhelper.AddMessage(minfo);

                            Tools.AlertAndRedirect("发工资成功", "UserList.aspx?id="+companyid);
                        }
                        else
                        {
                            Tools.Alert("不是该小组组长没有权限发工资");
                            return;
                        }
                    }
                }
            }
        }
        #endregion

        #region 返回
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserList.aspx?id="+companyid);
            return;
        }
        #endregion
    }
}